// @ts-nocheck
import {createRouter, createWebHistory} from "vue-router";
import {AdminRouter} from "@/config/router/admin";
import LoginRouter from "@/config/router/login";
import useAdminLayoutStore from "@/store/admin/layout/AdminLayoutStore.ts";
import Router from "@/common/Router.ts";

const router = createRouter({
    history: createWebHistory(import.meta.env.VITE_BASE_URL),
    routes: [
        ...AdminRouter,
        ...LoginRouter,
        // 所有未定义路由，全部重定向到404页，必须放在最后, 404 页面的路由在 BasicRouter 里面
        {
            path: '/:path(.*)',
            redirect: '/404'
        },
    ]
})

router.beforeEach((to, from, next) => {
    if (to.path != "/" && to.path != "/login"){
        const store = useAdminLayoutStore()
        let routerList: Array<Router> = []
        store.ergodicRouter(to.matched, routerList);
        console.log(routerList[1])
        store.routerToItem(routerList[1], store.getRouterItem())
        store.setRouterItemList(store.itemToList(store.getRouterItem(), []))
        localStorage.getItem("token")
    }else {
        localStorage.removeItem("token")
    }
    next()
})
export default router;